package jzqb.controller;


import haha.life.common.SysTool;
import haha.life.normal.TokenUtils;
import jzqb.bo.CollectClassifyBo;
import jzqb.common.Result;
import jzqb.service.CollectClassifyService;
import org.apache.logging.log4j.util.PerformanceSensitive;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/collectClassify")
public class CollectClassifyController {


    @Autowired
    CollectClassifyService collectClassifyService;

    //新建收藏分类
    @PostMapping(value = "/insertCollectClassify")
    public Result insertCollectClassify(@RequestBody CollectClassifyBo collectClassifyBo, HttpServletRequest request) {
        String userId = TokenUtils.getCurrentUserId(request);
        collectClassifyBo.setUserID(userId);
        CollectClassifyBo ress = collectClassifyService.getCollectClassifyByName(collectClassifyBo);
        if (ress != null) {
            return Result.fail("请勿重复新建相同分类");
        }
        Boolean res = collectClassifyService.insertCollectClassify(collectClassifyBo);
        if (res) {
            CollectClassifyBo result = collectClassifyService.getCollectClassifyByName(collectClassifyBo);
            return Result.succ(result);
        }
        return Result.fail("新建收藏分类失败");

    }

    //新建收藏分类
    @PostMapping(value = "/updateCollectClassify")
    public Result updateCollectClassify(@RequestBody CollectClassifyBo collectClassifyBo, HttpServletRequest request) {
        String userId = TokenUtils.getCurrentUserId(request);
        collectClassifyBo.setUserID(userId);
        CollectClassifyBo ress = collectClassifyService.getCollectClassifyByName(collectClassifyBo);
        if (ress != null) {
            return Result.fail("收藏分类名称不能相同");
        }
        Boolean res = collectClassifyService.updateCollectClassify(collectClassifyBo);
        if (res) {
            return Result.succ("修改收藏分类成功");
        }
        return Result.fail("修改收藏分类失败");
    }


    //删除收藏分类
    @PostMapping(value = "/deleteCollectClassify")
    public Result deleteCollectClassify(@RequestBody CollectClassifyBo collectClassifyBo, HttpServletRequest request) {
        String userId = TokenUtils.getCurrentUserId(request);
        collectClassifyBo.setUserID(userId);
        Boolean res = collectClassifyService.deleteCollectClassify(collectClassifyBo);
        if (res) {
            return Result.succ("删除收藏分类成功");
        }
        return Result.fail("删除收藏分类失败");
    }

    //获取收藏分类列表
    @PostMapping(value = "/getCollectClassifyList")
    public Result getCollectClassifyList(HttpServletRequest request) {
        String userId = TokenUtils.getCurrentUserId(request);
        List<CollectClassifyBo> list = collectClassifyService.getCollectClassifyList(userId);
        return Result.succ(list);
    }
}
